package util;

import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class MyButton extends JPanel {

	private MouseListener[] mouseListeners;
	private MouseMotionListener[] mouseMotionListeners;
	private String toolTipText;

	public MyButton(JPanel panel, String fileAdress) {
		GridLayout pnlOpenLayout = new GridLayout(1, 1);
		this.setLayout(pnlOpenLayout);
		panel.add(this);
		ImageIcon icon = UtilClass.createImageIcon(fileAdress);
		this.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
		JLabel lbl = new JLabel(icon);
		this.add(lbl);
		this.addMouseListener(new MouseAdapter() {
			public void mouseExited(MouseEvent evt) {
				pnlOpenMouseExited(evt);
			}

			public void mouseEntered(MouseEvent evt) {
				pnlOpenMouseEntered(evt);
			}
		});
	}

	private void pnlOpenMouseEntered(MouseEvent evt) {
		this.setBorder(new LineBorder(null, 1));
	}

	private void pnlOpenMouseExited(MouseEvent evt) {
		this.setBorder(null);
	}

	public void disableListeners() {
		mouseListeners = this.getMouseListeners();
		mouseMotionListeners = this.getMouseMotionListeners();
		toolTipText = this.getToolTipText();
		this.setToolTipText(null);
		for (MouseListener ml : mouseListeners) {
			this.removeMouseListener(ml);
		}
		for (MouseMotionListener mml : mouseMotionListeners) {
			this.removeMouseMotionListener(mml);
		}
	}

	public void enableListeners() {
		for (MouseListener ml : mouseListeners) {
			this.addMouseListener(ml);
		}
		for (MouseMotionListener mml : mouseMotionListeners) {
			this.addMouseMotionListener(mml);
		}
		this.setToolTipText(toolTipText);
	}
}